package com.devin.controller;

import com.devin.pojo.Permission;
import com.devin.service.PermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.ModelAndViewResolverMethodReturnValueHandler;

import java.util.List;

@Controller
@RequestMapping("/permissions")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/getPermissions")
    public ModelAndView getPermissions(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                       @RequestParam(value = "size",defaultValue = "5")Integer size){
        List<Permission> permissions = permissionService.getPermissionsByPage(page,size);
        PageInfo<Permission> permissionPageInfo = new PageInfo<>(permissions);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("permissionPageInfo",permissionPageInfo);
        modelAndView.setViewName("pages/permission-pages-list");
        return modelAndView;
    }

    @GetMapping("/toAddPermission")
    public String toAddPermission(){
        return "pages/permission-add";
    }

    @RequestMapping("/addPermission")
    public String addPermission(Permission permission){
        permissionService.addPermission(permission);
        return "redirect:/permissions/getPermissions";
    }

    @RequestMapping("/delPermission")
    public String delPermission(Integer permissionId){
        permissionService.delPermission(permissionId);
        return "redirect:/permissions/getPermissions";
    }

    @RequestMapping("/getPermissions2Json")
    @ResponseBody
    public List<Permission> getPermissions2Json(Integer roleId){
        List<Permission> permissions = permissionService.getPermissions2Json(roleId);
        return permissions;
    }

    @PostMapping("/delPermissionsChecked")
    @ResponseBody
    public String delPermissionsChecked(Integer[] ids){
        permissionService.delPermissionsChecked(ids);
        return "good";
    }
}
